<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/5/24 0024
 * Describe: 立时贷回调管理类 所有回调给立时贷的都在这.
 */

namespace app\api\controller\lishidai;

use think\Controller;
use think\Log;

class Callback extends Controller
{
    public function __construct()
    {
        parent::__construct();

        //日志初始化保存地址
        $setPath = strtolower(request()->module().'/'.request()->controller().'/'.request()->action().'/');
        Log::init(['path' => LOG_PATH.$setPath, 'type' => 'file']);
    }

    /**
     * 审批结果.
     */
    public function approveFeedback($data = [])
    {
        $data = [
            'order_sn' => '245132241561415', //订单唯一编号
            'approve_status' => '200', //200 审批通过 403审批拒绝
            'approve_amount' => 150000, //审批后的可借金额单位（分）
            'approve_term' => 7, //审批后的可借周期
            'term_type' => 1, //1:按天; 2：按月; 3：按年
            'approve_remark' => '', //审批状态备注
            'can_loan_time' => '2019-05-24', //可以再次申请借款的时间，此时间之前将不在请求用户过滤接口。值如：2017-03-29 08:00:00
            'updated_at' => '2019-05-21', //订单状态变更时间(10位时间戳)
        ];

        $temp = $this->getBasicsParam($data);
    }

    /**
     * 获取基础参数.
     */
    public function getBasicsParam()
    {
        $temp = [
            'ua' => 'aaa',
            'call' => 'xxxx',
            'args' => [],
            'sign' => 'bbbbbbbbbbbbbb',
            'timestamp' => time(),
        ];

        return $temp;
    }

    /**
     * 获取配置立时贷配置参数 ua key.
     *
     * @return array
     */
    public function getLishidaiConfig()
    {
        $data = db('sys_integrate')->alias('i')
            ->join('xb_sys_inteparameter in', 'i.ID = in.IntegrateID')
            ->where(['i.Status' => 1, 'i.EName' => 'lishidai'])
            ->select();

        if (!$data) {
            errorResult('未配置立时贷');
        }

        $temp = [];
        foreach ($data as $k => $v) {
            $temp[$v['ParaName']] = $v['ParaValue'];
        }

        if (!$temp || !isset($temp['lishidai_ua']) || empty($temp['lishidai_ua'])) {
            errorResult('未配置立时贷平台接入标识 ua');
        } elseif (!$temp || !isset($temp['lishidai_key']) || empty($temp['lishidai_key'])) {
            errorResult('未配置立时贷签名密钥 signkey');
        } elseif (!$temp || !isset($temp['lishidai_url']) || empty($temp['lishidai_url'])) {
            errorResult('未配置立时贷 url');
        }

        return $temp;
    }
}
